/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Controladores;

import Beans.Compra;
import Beans.Proveedor;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Usuario
 */
public class Mcompra {

    
     private  List <Compra> compras = new LinkedList<Compra>();//lista
     Compra c =new Compra();
     File archivo=new File("Compras.dat");
     
      public void grabar() throws IOException {

      FileOutputStream   flujoSalida  = new FileOutputStream(archivo);
      ObjectOutputStream objetoSalida = new ObjectOutputStream(flujoSalida);
      objetoSalida.writeObject(this.getCompras());
      objetoSalida.close();
     }


    public void restaurar() throws IOException,ClassNotFoundException {
        if(archivo.exists()==true){
            FileInputStream flujoEntrada    = new FileInputStream(archivo);
            ObjectInputStream objetoEntrada = new ObjectInputStream(flujoEntrada);
            List<Compra> cr = (List) objetoEntrada.readObject();
            this.setCompras(cr);
            objetoEntrada.close();
        }
 	
    }

    public List<Compra> getCompras() {
        return compras;
    }

    public void setCompras(List<Compra> compras) {
        this.compras = compras;
    }

   public int GenerarCodigo(){
        int codigo= 0;
        int limite = 10000;
        Random r = new Random();
        codigo = r.nextInt(limite+1);
       return codigo;

    }
     

    public Boolean Existe(int cod){
        boolean sw=false;
        Iterator it = compras.iterator();
        while(it.hasNext()){
            c = (Compra)it.next();
            if(c.getCodigo()==cod){
                sw=true;
            }
        }
        return sw;
    }


      public void Modificar(int pos,int cod,Compra compra){
       Iterator itc=compras.iterator();
         while(itc.hasNext()){
             c = (Compra)itc.next();
            if(c.getCodigo() == cod){
                compras.set(pos, compra);
                JOptionPane.showMessageDialog(null,"Los datos se guardaron "
                                                 + "Satisfactoriamente");
            }
        }
       }

     public void Registrar(Compra c){
         if(!Existe(c.getCodigo())){
             compras.add(c);
          }
      }

    public Compra Consultar(int cod){
        Iterator it = compras.iterator();
        while(it.hasNext()){
            Compra c = (Compra)it.next();
              if(c.getCodigo() == cod){
                 return c;
              }else{
                 JOptionPane.showMessageDialog(null,"Proveedor no encontrado");
              }
         }
        return new Compra();
     }
    public Compra ConsultarString(String prod){
        Iterator it = compras.iterator();
        while(it.hasNext()){
            Compra c = (Compra)it.next();
              if(c.getProducto().equals(prod)){
                 return c;
              }else{
                 JOptionPane.showMessageDialog(null,"Proveedor no encontrado");
              }
         }
        return new Compra();
     }

     public void Eliminar(int cod){
        try{
            Iterator it = compras.iterator();
            while(it.hasNext()){
                c =(Compra)it.next();
                if(c.getCodigo()== cod){
                   compras.remove(c);
                    try {
                        this.grabar();
                    } catch (IOException ex) {
                        Logger.getLogger(Mcompra.class.getName()).log(Level.SEVERE, null, ex);
                    }
                   JOptionPane.showMessageDialog(null,"Eliminado");
                }
            }
        }catch(ConcurrentModificationException e){

        }
     }

 }
















